<?php

class Frontend {

    private static $_instance;

    /**
     * 
     * @return Mongo_Mysql
     * 
     */
    private static function factory() {
        if (!self::$_instance) {
            self::$_instance = new Mongo_Mysql();
        }
        return self::$_instance;
    }

    /**
     * 
     * @param type $email
     * @param type $status
     * @return type
     * 
     */
    public static function findAllUser($email = "", $status = 0) {
        $arr = array("email" => array('$regex' => $email));
        return self::factory()->find("users", $arr);
    }

    /**
     * 
     * @param type $id
     * @return type
     * 
     */
    public static function findUserById($id) {
        $arr = array("_id" => $id);
        return self::factory()->find("users", $arr);
    }

    /**
     * 
     * @param type $email
     * @param type $pass
     * @return type
     * 
     */
    public static function findUserLogin($email, $pass) {
        $arr = array("email" => $email, "password" => md5($pass));
        return self::factory()->find("users", $arr);
    }

    /**
     * 
     * @param type $username
     * @param type $password
     * @return type
     */
    public static function findAdminLogin($username, $password) {
        $arr = array("username" => $username, "password" => md5($password));
        return self::factory()->find("users", $arr);
    }

    /**
     * 
     * @param type $email
     * @return $data
     * 
     */
    public static function findUserByEmail($email) {
        $arr = array("email" => $email);
        return self::factory()->find("users", $arr);
    }

    /**
     * 
     * @param type $pass
     * @return type
     * 
     */
    public static function findUserByPass($pass, $id) {
        $arr = array("password" => md5($pass), "_id" => $id);
        return self::factory()->find("users", $arr);
    }

    /**
     * 
     * @return type
     * 
     */
    public static function findAllCountry($name = "", $status = "1", $sort = "asc") {
        $arr = array("name" => array('$regex' => $name), "status" => $status);
        if ($sort == "asc")
            $sort = array("name" => 1);
        if ($sort == "desc")
            $sort = array("name" => -1);
        return self::factory()->find("country")->sort($sort);
    }

    /**
     * 
     * @param type $id
     * @return type
     * 
     */
    public static function findOneCountry($id, $status = 1) {
        $arr = array("_id" => $id, "status" => $status);
        return self::factory()->find("country", $arr);
    }

    /**
     * 
     * @return type
     * 
     */
    public static function findAllTournament($title = "", $status = 1) {
        $arr = array("title" => array('$regex' => $title), "status" => $status);
        return self::factory()->find("tournament", $arr);
    }

    /**
     * 
     * @param type $id
     * @param type $status
     * @return type
     * 
     */
    public static function findTournamentById($id, $status = 1) {
        $arr = array("_id" => $id, "status" => $status);
        return self::factory()->find("tournament", $arr);
    }

    /**
     * 
     * @param type $url
     * @param type $status
     * @return type
     */
    public static function findTournamentByUrl($url, $status = 1) {
        $arr = array("location" => array('$regex' => $url), "status" => $status);
        return self::factory()->find("tournament", $arr);
    }

    /**
     * 
     * @param type $country_id
     * @return type
     * 
     */
    public static function findTournamentByCountry($country_id, $status = 1) {
        $arr = array("countryID" => $country_id, "status" => $status);
        return self::factory()->find("tournament", $arr);
    }

    /**
     * 
     * @param type $status
     * @return type
     * 
     */
    public static function findAllMacth($status = 0) {
        $arr = array("status" => $status);
        return self::factory()->find("match", $arr);
    }

    /**
     * 
     * @param type $id
     * @return type
     * 
     */
    public static function findMatchById($id) {
        $arr = array("_id" => $id);
        return self::factory()->find("match", $arr);
    }

    /**
     * 
     * @param type $curent_date
     * @param type $status
     * 
     */
    public static function findMatchByTimeStamp($curent_date, $status = 0, $sort = 'asc') {
        $arr = array("DateMatchTS" => array('$gte' => $curent_date), "status" => $status);
        if ($sort == "asc")
            $sort = array("DateMatchTS" => 1);
        if ($sort == "desc")
            $sort = array("DateMatchTS" => -1);
        return self::factory()->find("match", $arr)->sort($sort);
    }
    
    public static function findMatchByTimeStampToDate($begin_date, $end_date, $status = 0, $sort = 'asc') {
        $arr = array("DateMatchTS" => array('$gte' => $begin_date, '$lte' => $end_date), "status" => $status);
        if ($sort == "asc")
            $sort = array("DateMatchTS" => 1);
        if ($sort == "desc")
            $sort = array("DateMatchTS" => -1);
        return self::factory()->find("match", $arr)->sort($sort);
    }
    
    public static function findMatchByLevel($curent_date, $status = 0, $sort = 'asc') {
        $arr = array("DateMatchTS" => array('$gte' => $curent_date), "status" => $status);
        if ($sort == "asc")
            $sort = array("level" => 1);
        if ($sort == "desc")
            $sort = array("level" => -1);
        return self::factory()->find("match", $arr)->sort($sort);
    }
    /**
     * 
     * @param type $tourId
     * @param type $curent_date
     * @param type $status
     * @param type $sort
     * @return type
     * 
     */
    public static function findMatchByTourIdTimeStamp($tourId, $curent_date, $status = 0, $sort = 'asc') {
        $arr = array("TourID" => $tourId, "DateMatchTS" => array('$gte' => $curent_date), "status" => $status);
        if ($sort == "asc")
            $sort = array("DateMatchTS" => 1);
        if ($sort == "desc")
            $sort = array("DateMatchTS" => -1);
        return self::factory()->find("match", $arr)->sort($sort);
    }

//    public static function findMatchGroup($key){
//        $arr = array("DateMatchTS" => array('$gt' => (int)$curent_date), "status" => $status);
//        return self::factory()->group("match", $arr);
//    }
    /**
     * 
     * @param type $curent_date
     * @return type
     * 
     */
    public static function findMatchCurrentDate($curent_date, $status = 0, $sort = 'asc') {
        $arr = array("DateMatch" => $curent_date, "status" => $status);
        if ($sort == "asc")
            $sort = array("DateMatch" => 1);
        if ($sort == "desc")
            $sort = array("DateMatch" => -1);
        return self::factory()->find("match", $arr)->sort($sort);
    }

    /**
     * 
     * @param type $tour_id
     * @param type $status
     * @return type
     * 
     */
    public static function findMacthByTournament($tour_id, $status = 0) {
        $arr = array("TourID" => $tour_id, "status" => $status);
        return self::factory()->find("match", $arr);
    }

    /**
     * 
     * @param type $start_time
     * @param type $status
     * @return type
     * 
     */
    public static function findMatchByTime($start_time, $status = 0) {
        $arr = array("StartTime" => $start_time, "status" => $status);
        return self::factory()->find("match", $arr);
    }

    /**
     * 
     * @param type $date
     * @param type $status
     * @param type $sort : 
     *      acs  : sắp xếp tăng dần ---
     *      desc : sắp xếp giảm dần
     * @return type
     */
    public static function findMatchByDate($date, $status = 0, $sort = 'asc') {
        $arr = array("DateMatch" => array('$regex' => $date), "status" => $status);
        if ($sort == "asc")
            $sort = array("StartTime" => 1);
        if ($sort == "desc")
            $sort = array("StartTime" => -1);
        return self::factory()->find("match", $arr)->sort($sort);
    }

    /**
     * 
     * @param type $id
     * @return type
     * 
     */
    public static function findOddById($id) {
        $arr = array("_id" => $id);
        return self::factory()->find("odd", $arr);
    }

    /**
     * 
     * @param type $value
     * @return type
     * 
     */
    public static function findOddByValue($value) {
        $arr = array("value" => array('$regex' => $value));
        return self::factory()->find("odd", $arr);
    }

    /**
     * 
     * @param type $match_id
     * @return type
     * 
     */
    public static function findOddByMacth($match_id) {
        $arr = array("matchID" => $match_id);
        return self::factory()->find("odd", $arr);
    }

    /**
     * 
     * @param type $type
     * @return type
     * 
     */
    public static function findOddByType($type) {
        $arr = array("typeID" => "$type");
        return self::factory()->find("odd", $arr);
    }

    /**
     * 
     * @param type $status
     * @return type
     * 
     */
    public static function findAllTeam($status = 0) {
        $arr = array("status" => $status);
        return self::factory()->find("team", $arr);
    }

    /**
     * 
     * @param type $team_id
     * @return type
     * 
     */
    public static function findTeamById($team_id) {
        $arr = array("_id" => $team_id);
        return self::factory()->find("team", $arr);
    }

    public static function findAllMatchResult() {
        return self::factory()->find("matchresult");
    }

    /**
     * 
     * @param type $curent_date
     * @param type $status
     * @param type $sort
     * @return type
     * 
     */
    public static function findMatchResultTimeStamp($curent_date, $sort = 'asc') {
        $arr = array("datematchTS" => array('$lte' => $curent_date));
        if ($sort == "asc")
            $sort = array("datematchTS" => 1);
        if ($sort == "desc")
            $sort = array("datematchTS" => -1);
        return self::factory()->find("matchresult", $arr)->sort($sort);
    }
    /**
     * 
     * @param type $tourId
     * @param type $curent_date
     * @param type $sort
     * @return type
     * 
     */
    public static function findMatchResultByTourIdTimeStamp($tourId, $curent_date, $sort = 'asc') {
        $arr = array("tourid" => $tourId, "datematchTS" => array('$lte' => $curent_date));
        if ($sort == "asc")
            $sort = array("datematchTS" => 1);
        if ($sort == "desc")
            $sort = array("datematchTS" => -1);
        return self::factory()->find("matchresult", $arr)->sort($sort);
    }
    /**
     * 
     * @param type $table
     * @param type $data
     * @param type $id
     * 
     */
    public static function onUpdate($table, $data, $id) {
        self::factory()->update($table, $data, $id);
    }
    /**
     * 
     * @param type $user_id
     * @param type $sort
     * @return type
     * 
     */
    public static function findUserOddByUid($user_id, $sort = 'asc'){
        $arr = array("userid" => $user_id);
        if ($sort == "asc")
            $sort = array("datematchTS" => 1);
        if ($sort == "desc")
            $sort = array("datematchTS" => -1);
        return self::factory()->find("userodd", $arr)->sort($sort);
    }
    
    public static function findUserOddByMatchId($match_id, $status = 0){
        $arr = array("match_id" => $match_id, "status" => $status);
        return self::factory()->find("userodd", $arr);
    }
    /**
     * 
     * @param type $user_id
     * @param type $sort
     * @return type
     * 
     */
    public static function findUserOddSxByUid($user_id, $sort = 'asc'){
        $arr = array("userid" => $user_id);
        if ($sort == "asc")
            $sort = array("datematchTS" => 1);
        if ($sort == "desc")
            $sort = array("datematchTS" => -1);
        return self::factory()->find("useroddxs", $arr)->sort($sort);
    }
    
    public static function findOddSxMB($yesterday, $location, $status = 1){
        $arr = array("date" => array('$regex' => $yesterday), "location" => $location, "status" => $status);
        return self::factory()->find("oddxs", $arr);
    }
    
    public static function findPrize($status = 1){
        $arr = array("status" => $status);
        return self::factory()->find("prize", $arr);
    }
    
    public static function findPrizeById($prize_id, $status = 1){
        $arr = array("_id" => $prize_id,"status" => $status);
        return self::factory()->find("prize", $arr);
    }
    
    public static function findAllNews($status = 1)  {
        $arr = array("status" => $status);
        return self::factory()->find("news", $arr);
    }
    
    public static function findNewsById($news_id, $status = 1)  {
        $arr = array("_id" => $news_id,"status" => $status);
        return self::factory()->find("news", $arr);
    }
    
    public static function findUserByCode($code){
        $arr = array("activecode" => $code);
        return self::factory()->find("users", $arr);
    }
}

?>
